//
//  ShareViewController.m
//  wtshoppingcite
//
//  Created by TW on 15-3-26.
//  Copyright (c) 2015年 TW. All rights reserved.
//

#import "ShareViewController.h"
#import "ShareTableView.h"
#import "AdispatchView.h"
#import "PostViewController.h"
#import "ShareModel.h"
#import "PariseViewController.h"

@interface ShareViewController ()<UITableViewEventsDelegate,adispathViewDelegate>
{

    BOOL isClick;
}
@property(nonatomic,strong)ShareTableView *shareTableView;
@property(nonatomic,strong)NSString *expectEndTime;
@property(nonatomic,strong)AdispatchView *adispatchView;
@property(nonatomic,strong)NSString *pageNum;
@property(nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation ShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArray = [[NSMutableArray alloc]initWithCapacity:1];
    self.navigationItem.leftBarButtonItem = nil;
    
    _pageNum = @"0";
    [self getShareList:NO];
    
    [self initWithView];
    
    
    // Do any additional setup after loading the view.
}

-(void)initWithView{
    
    
    [self setTitleView:@"分享"];
    
    //isClick = YES;
    
    UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 18, 15)];
    [button setImage:[UIImage imageNamed:@"share_photo"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    _shareTableView = [[ShareTableView alloc]initWithFrame:CGRectMake(0,0, kWidth, kHeight-64) style:UITableViewStylePlain];
    _shareTableView.isNeedTorefresh = YES;
    _shareTableView.eventsDelegate = self;
    _shareTableView.isMore = YES;
    _shareTableView.backgroundColor = COLOR_RGB(224, 224, 224);
    
    [self.view addSubview:_shareTableView];
}


-(void)viewWillAppear:(BOOL)animated{
    
    _pageNum = @"0";
    [self getShareList:NO];
    [MobClick beginLogPageView:@"PageOne"];
}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"PageOne"];
}


#pragma mark caremaAction
-(void)clickAction{
    
    //    if (isClick) {
    _adispatchView = [[AdispatchView alloc]initWithFrame:CGRectMake(20, 95, kWidth - 40, 44*7)];
    [_adispatchView showView:APP_DELEGATE.window];
    _adispatchView.delegate = self;
    _adispatchView.titleArray = @[@"意见反馈",@"表扬",@"好人好事",@"帮帮忙",@"跳蚤市场",@"美食",@"其它"];
    [APP_DELEGATE.window addSubview:_adispatchView];
}


#pragma mark adispatchViewDlegate
-(void)adispathView:(NSString *)title{
    
    if([title isEqual:@"意见反馈"]){
        _expectEndTime = @"1";
    }else if([title isEqual:@"表扬"]){
        _expectEndTime = @"2";
    }else if([title isEqual:@"好人好事"]){
        _expectEndTime = @"3";
    }else if([title isEqual:@"帮帮忙"]){
        _expectEndTime = @"4";
    }else if([title isEqual:@"跳蚤市场"]){
        _expectEndTime = @"5";
    }else if([title isEqual:@"美食"]){
        _expectEndTime = @"6";
    }else if([title isEqual:@"其它"]){
        _expectEndTime = @"7";
    }
    
    PostViewController *postVC = [[PostViewController alloc]init];
    postVC.hidesBottomBarWhenPushed = YES;
    postVC.title = title;
    postVC.titleType = _expectEndTime;
    [self.navigationController pushViewController:postVC animated:YES];
    
}

#pragma mark  eventsDelegate
- (void)pullUp:(BaseTableView *)baseTableView{
    
    [self getShareList:YES];
   
}

- (void)pullDown:(BaseTableView *)baseTableView{
    
    _pageNum = @"0";
    [self getShareList:NO];

}

#pragma mark getShareList
-(void)getShareList:(BOOL)isMore{
    NSString *cid = [RegisterHelper shareHelper].citesId;
    NSString *page = _pageNum;
    NSString *pageSize = @"10";
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    SetValueForKey(dic, cid, @"cid");
    SetValueForKey(dic, page, @"page");
    SetValueForKey(dic, pageSize, @"pageSize");
    
    [self showLoadImageView:self.view subview:_shareTableView];
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                              andAPISuffix:kShareList
                             andHttpMethod:@"POST"
                             andParameters:dic
                           andRequestCache:NO
                            andUseParamDic:YES
                       andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                     andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                         
                         [_shareTableView doneLoadingTableViewData];
                         [self performSelector:@selector(dismissLoadImageView) withObject:nil afterDelay:0.5];
                         
                         request.requestCache = YES;
                         
                         if (response.isError) {
                             
                             DLog(@"error %@",response.errorMessage);
                             
                             
                         }else{
                             NSDictionary *responseDic = request.responseJSON;
                             DLog(@"成功数据:%@",responseDic);
                             
                             NSArray *list = [responseDic objectForKey:@"list"];
                             if (list.count < 10) {
                                 _shareTableView.isMore = NO;
                             }else{
                                 _shareTableView.isMore = YES;
                                 
                                 int currentPage = [_pageNum intValue];
                                 _pageNum = [NSString stringWithFormat:@"%d",currentPage+1];
                                 
                             }
                             if (!isMore) {
                                 [_dataArray removeAllObjects];
                                 
                             }
                             for (NSDictionary *dic in list) {
                                 ShareModel *shareModel = [[ShareModel alloc]initWithShareInfo:dic];
                                 [_dataArray addObject:shareModel];
                             }
                             _shareTableView.data = _dataArray;
                         }
                         
                         [_shareTableView reloadData];

                     }];
    
}

-(void)basetableView:(BaseTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ShareModel *shareModel = _dataArray[indexPath.row];
    
    PariseViewController *postVC = [[PariseViewController alloc]init];
    postVC.hidesBottomBarWhenPushed = YES;
    postVC.shareId = shareModel.bid;
    [self.navigationController pushViewController:postVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
